package com.jdk.demo.fileChannel.read;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Test2_6 {

	// 验证read方法从通道读取的字节放入缓冲区的remaining空间
	public static void main(String[] args) throws Exception {
		FileInputStream fisRef = new FileInputStream("b.txt");
		FileChannel fileChannel = fisRef.getChannel();
		
		ByteBuffer bytebuffer = ByteBuffer.allocate(100);
		bytebuffer.position(1);
		bytebuffer.limit(3);
		fileChannel.read(bytebuffer);
		
		bytebuffer.rewind();
		// 输出"空格ab"
		for (int i = 0; i < bytebuffer.limit(); i++) {
			byte eachByte = bytebuffer.get();
			if(eachByte == 0){
				System.out.print("空格");
			}else{
				System.out.print((char)eachByte);
			}
		}
		fileChannel.close();
		fisRef.close();
	}
	
}
